package com.qf.myrbac.controller;

import com.baomidou.kaptcha.Kaptcha;
import com.qf.myrbac.utils.ResultMap;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@RequestMapping("/sys")
public class LoginController {
    @Autowired
    private Kaptcha kaptcha;

    @PostMapping("/login")
    public ResultMap login(String username, String password, String captcha) {
        log.debug("username:{},password:{},captcha:{}", username, password, captcha);
        if (!kaptcha.validate(captcha)) {
            return ResultMap.error("验证码错误");
        }
        Subject subject = SecurityUtils.getSubject();
        subject.login(new UsernamePasswordToken(username, password));
        return ResultMap.ok("登陆成功");
    }
}
